package controller.editor;

import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

import view.editor.EditorView;

public class ProjectExplorerController implements TreeSelectionListener{

	private EditorView view;
	private JTree tree;
	
	public ProjectExplorerController(JTree tree, EditorView view){
		this.tree = tree;
		this.view = view;
	}
	
	@Override
	public void valueChanged(TreeSelectionEvent arg) {
		DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)this.tree.getLastSelectedPathComponent();
		if (selectedNode != null){
			String selection = selectedNode.toString();
			if (view.getController().existsDiagram(selection)){
				this.view.getController().setNewCurrentDiagram(selection);
			} else {
				if (!view.getController().getProject().getName().equals(selection)){
					DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)selectedNode.getParent();
					this.view.getController().setNewCurrentDiagram(parentNode.toString());
				}
			}
		}
	}

}
